package com.testMaven;

import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Login {
	private WebDriver driver;
    private boolean result;
    
	public  boolean testLogin(String userName, String passWord) throws Exception{
		
		//new fireFox驱动
				driver = new FirefoxDriver();
				//访问登录页
				driver.get("https://passport.beta1.fn/gateway/login");
				//定位元素
				WebElement user = driver.findElement(By.name("username"));
				WebElement pwd  = driver.findElement(By.name("password"));
				WebElement sub  = driver.findElement(By.name("sbtbtn"));
				
				//元素赋值
				user.clear();
				user.sendKeys(userName);
				pwd.clear();
				pwd.sendKeys(passWord);
				//点击登录按钮
				sub.click();
				Thread.sleep(500);
				
				/*
				 * 定位页面头部是否有[退出]
				 * 有:登录成功
				 * 未找到元素：登录失败
				 */
		       try{
					WebElement quit = driver.findElement(By.xpath("//*[@id='topbar-right']/li[3]/a"));
					if(quit.getText().equals("[退出]")){
						result = true;
					}
				}catch(NoSuchElementException e){
					result = false;
				}
				return result;
			
		}
			
			
			//窗口切换
				public void changeWindow(WebDriver driver){
				//得到当前窗口句柄
				String currentWindow = driver.getWindowHandle();
				//得到所有窗口句柄
				//Set<String> allWindow1 = driver.getWindowHandles();
				for(String allWindow : driver.getWindowHandles()){
					//排除当前窗口
					if (allWindow.equals(currentWindow)){
						continue;
					}else{
					//定位其他页面
					
					driver.switchTo().window(allWindow);
					break;
				    }
				}
			
			}
			//元素加载等待 
				/*创建WebDriverWait对象
				 * WebDriver driver:驱动
				 * long timeOutInSeconds：等待时间
				 * String path ：元素
				 * 获取元素
				*/
				public void elementWait(WebDriver driver,long timeOutInSeconds,String path){

					WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
					wait.until(ExpectedConditions.elementToBeClickable(By.xpath(path)));
			}
	
}
